package org.dinner.order.dao.impl;

import java.util.Date;
import java.util.List;

import org.dinner.order.dao.DishDAO;
import org.dinner.order.dao.MenuDAO;
import org.dinner.order.model.Dish;
import org.dinner.order.model.Menu;
import org.dinner.order.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class MenuDAOImpl extends BaseDAOImpl<Menu> implements MenuDAO {

	private static final String ENTITY_NAME = "Menu" ;
	
	@Autowired
	private DishDAO dishDAO;
	
	public Menu getByDate(Date date) {
		List list = sessionFactory.getCurrentSession().getNamedQuery(ENTITY_NAME + ".getByDate")
				.setDate("date", DateUtils.truncateTime(date)).list();
		
		if (list != null && list.size() > 0) {
			return (Menu) list.get(0);
		}
		
		return null;
	}

	public Menu createMenu(Date date, List<Dish> dishes) {
		Menu menu = new Menu();
		
		menu.setDate(DateUtils.truncateTime(date));
		menu.getDishes().addAll(dishes);
		
		add(menu);
		
		return menu;
	}

	public void addDish(int menuId, int dishId) {
		Menu menu = this.getById(menuId);
		Dish dish = dishDAO.getById(dishId);
		
		menu.getDishes().add(dish);
		
	}

	@Override
	protected String getQueryPrefix() {
		return ENTITY_NAME;
	}

}
